//
//  HomeCell1.m
//  Tunnel
//
//  Created by wbb on 2025/6/2.
//


#import "HomeCell1.h"
// CustomCollectionViewCell.m
//#import "CustomCollectionViewCell.h"

@interface CustomCollectionViewCell : UICollectionViewCell
@property (nonatomic, strong) UIImageView *imageView;
@property (nonatomic, strong) UILabel *titleLabel;
@end



@implementation CustomCollectionViewCell

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.contentView.backgroundColor = [UIColor clearColor];
        [self setupSubviews];
        //        [self setupConstraints];
    }
    return self;
}

- (void)setupSubviews {
    
    UIStackView *stackView = [[UIStackView alloc] init];
    stackView.axis = UILayoutConstraintAxisVertical; // 明确设置为垂直方向
    stackView.alignment = UIStackViewAlignmentCenter; // 居中对齐
    stackView.spacing = 5; // 设置子视图之间的间距
    [self.contentView addSubview:stackView];
    [stackView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self);
    }];
    
    // 创建图片视图
    self.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 32, 32)];
    self.imageView.contentMode = UIViewContentModeScaleAspectFit;
    //    [stackView addSubview:self.imageView];
    
    // 创建标题标签
    self.titleLabel = [[UILabel alloc] init];
    self.titleLabel.textAlignment = NSTextAlignmentCenter;
    self.titleLabel.font = [UIFont systemFontOfSize:14];
    //    [stackView addSubview:self.imageView];
    
    // 将视图添加到StackView
    [stackView addArrangedSubview:self.imageView];
    [stackView addArrangedSubview:self.titleLabel];
}

- (void)setupConstraints {
    self.imageView.translatesAutoresizingMaskIntoConstraints = NO;
    self.titleLabel.translatesAutoresizingMaskIntoConstraints = NO;
    
    // 设置图片视图约束
    [NSLayoutConstraint activateConstraints:@[
        [self.imageView.topAnchor constraintEqualToAnchor:self.contentView.topAnchor],
        [self.imageView.leadingAnchor constraintEqualToAnchor:self.contentView.leadingAnchor],
        [self.imageView.trailingAnchor constraintEqualToAnchor:self.contentView.trailingAnchor],
        [self.imageView.heightAnchor constraintEqualToAnchor:self.contentView.heightAnchor multiplier:0.7]
    ]];
    
    // 设置标题标签约束
    [NSLayoutConstraint activateConstraints:@[
        [self.titleLabel.topAnchor constraintEqualToAnchor:self.imageView.bottomAnchor],
        [self.titleLabel.leadingAnchor constraintEqualToAnchor:self.contentView.leadingAnchor],
        [self.titleLabel.trailingAnchor constraintEqualToAnchor:self.contentView.trailingAnchor],
        [self.titleLabel.bottomAnchor constraintEqualToAnchor:self.contentView.bottomAnchor]
    ]];
}

@end


@interface HomeCell1()<UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>
@property (nonatomic, copy) NSArray * dataArr;
@property (nonatomic, strong) UICollectionView *collectionView;

@end

@implementation HomeCell1

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    
    if(self) {
        self.backgroundColor = [UIColor clearColor];
       
        [self initUI];
        
        self.collectionView.cornerRadius = 7;

    }
    
    
    
    return  self;
    
}
- (void)layoutSubviews {
    [super layoutSubviews];
    [self.collectionView addVerticalGradientWithTopColor:hexColor(E0EAFF)
                                 bottomColor:hexColor(FFFFFF)];
}


- (void)initUI {
    
    
    
    // 创建CollectionView布局
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    CGFloat itemWidth = self.contentView.bounds.size.width / 4.f;
    layout.itemSize = CGSizeMake(itemWidth, 60);
    layout.minimumInteritemSpacing = 10;
    layout.minimumLineSpacing = 5;
    layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10); // 上、左、下、右各10点
    
    // 创建CollectionView
    self.collectionView = [[UICollectionView alloc] initWithFrame:self.contentView.bounds
                                             collectionViewLayout:layout];
    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;
    self.collectionView.backgroundColor = [UIColor clearColor];
   
    [self.collectionView registerClass:[CustomCollectionViewCell class]
            forCellWithReuseIdentifier:@"CustomCell"];
    [self.contentView addSubview:self.collectionView];
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.offset(0);
        make.left.offset(12);
        make.right.offset(-12);
    }];
}
#pragma mark - UICollectionViewDataSource

- (NSInteger)collectionView:(UICollectionView *)collectionView
     numberOfItemsInSection:(NSInteger)section {
    return self.dataArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView
                  cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    NSDictionary *dic = [NSDictionary dictionaryWithDictionary:self.dataArr[indexPath.row]];
    CustomCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CustomCell"
                                                                               forIndexPath:indexPath];
    
    // 设置图片和标题
    cell.imageView.image =[UIImage imageNamed:dic[@"imgUrl"]];
    cell.titleLabel.text = dic[@"title"];
    
    return cell;
}
#pragma mark - UICollectionViewDelegate

- (void)collectionView:(UICollectionView *)collectionView
didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"选中了: %d", indexPath.row);
    
    // 可以在这里添加点击事件处理逻辑
    if(self.ty_selectForIndex){
        self.ty_selectForIndex(indexPath.row);
    }
    
}


- (NSArray *)dataArr {
    if(!_dataArr){
        _dataArr = @[
            @{
                @"title":@"运行监测",
                @"imgUrl":@"yxjc_icon"
            },@{
                @"title":@"监控告警",
                @"imgUrl":@"jkgj_icon"
            },@{
                @"title":@"故障告警",
                @"imgUrl":@"gzgj_icon"
            },@{
                @"title":@"应急事件",
                @"imgUrl":@"yjsj_icon"
            },@{
                @"title":@"安全检查",
                @"imgUrl":@"aqjc_icon"
            },@{
                @"title":@"视频监控",
                @"imgUrl":@"spjk_icon"
            },
        ];
    }
    return _dataArr;
}
@end
